home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / chrome_100_percent.pak / Unnamed File 000026.txt < prev    next >
Text File  |  2013-04-03  |  5KB  |  157 lines

  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. // Custom bindings for the Bluetooth API.
  6.  
  7. var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
  8. var sendRequest = require('sendRequest').sendRequest;
  9. var lastError = require('lastError');
  10.  
  11. // Use custom bindings to create an undocumented event listener that will
  12. // receive events about device discovery and call the event listener that was
  13. // provided with the request to begin discovery.
  14. chromeHidden.registerCustomHook('bluetooth', function(api) {
  15.   var apiFunctions = api.apiFunctions;
  16.  
  17.   chromeHidden.bluetooth = {};
  18.  
  19.   function callCallbackIfPresent(args) {
  20.     if (typeof(args[args.length-1]) == "function") {
  21.       args[args.length-1]();
  22.     }
  23.   }
  24.  
  25.   chromeHidden.bluetooth.deviceDiscoveredHandler = null;
  26.   chromeHidden.bluetooth.onDeviceDiscovered =
  27.       new chrome.Event("bluetooth.onDeviceDiscovered");
  28.   function clearDeviceDiscoveredHandler() {
  29.     chromeHidden.bluetooth.onDeviceDiscovered.removeListener(
  30.         chromeHidden.bluetooth.deviceDiscoveredHandler);
  31.     chromeHidden.bluetooth.deviceDiscoveredHandler = null;
  32.   }
  33.   apiFunctions.setHandleRequest('startDiscovery',
  34.       function() {
  35.         var args = arguments;
  36.         if (args.length > 0 && args[0] && args[0].deviceCallback) {
  37.           if (chromeHidden.bluetooth.deviceDiscoveredHandler != null) {
  38.             lastError.set("Concurrent discovery is not allowed.");
  39.             callCallbackIfPresent(args);
  40.             return;
  41.           }
  42.  
  43.           chromeHidden.bluetooth.deviceDiscoveredHandler =
  44.               args[0].deviceCallback;
  45.           chromeHidden.bluetooth.onDeviceDiscovered.addListener(
  46.               chromeHidden.bluetooth.deviceDiscoveredHandler);
  47.           sendRequest(this.name,
  48.                       args,
  49.                       this.definition.parameters,
  50.                       {customCallback:this.customCallback});
  51.         } else {
  52.           lastError.set("deviceCallback is required in the options object");
  53.           callCallbackIfPresent(args);
  54.         }
  55.       });
  56.   apiFunctions.setCustomCallback('startDiscovery',
  57.       function(name, request, response) {
  58.         if (chrome.runtime.lastError) {
  59.           clearDeviceDiscoveredHandler();
  60.           return;
  61.         }
  62.       });
  63.   apiFunctions.setHandleRequest('stopDiscovery',
  64.       function() {
  65.         clearDeviceDiscoveredHandler();
  66.         sendRequest(this.name, arguments, this.definition.parameters);
  67.       });
  68.  
  69.   // An object to hold state during one call to getDevices.
  70.   chromeHidden.bluetooth.getDevicesState = null;
  71.  
  72.   // Hidden events used to deliver getDevices data to the client callbacks
  73.   chromeHidden.bluetooth.onDeviceSearchResult =
  74.       new chrome.Event("bluetooth.onDeviceSearchResult");
  75.   chromeHidden.bluetooth.onDeviceSearchFinished =
  76.       new chrome.Event("bluetooth.onDeviceSearchFinished");
  77.  
  78.   function deviceSearchResultHandler(device) {
  79.     chromeHidden.bluetooth.getDevicesState.actualEvents++;
  80.     chromeHidden.bluetooth.getDevicesState.deviceCallback(device);
  81.     maybeFinishDeviceSearch();
  82.   }
  83.  
  84.   function deviceSearchFinishedHandler(info) {
  85.     chromeHidden.bluetooth.getDevicesState.expectedEventCount =
  86.         info.expectedEventCount;
  87.     maybeFinishDeviceSearch();
  88.   }
  89.  
  90.   function addDeviceSearchListeners() {
  91.     chromeHidden.bluetooth.onDeviceSearchResult.addListener(
  92.         deviceSearchResultHandler);
  93.     chromeHidden.bluetooth.onDeviceSearchFinished.addListener(
  94.         deviceSearchFinishedHandler);
  95.   }
  96.  
  97.   function removeDeviceSearchListeners() {
  98.     chromeHidden.bluetooth.onDeviceSearchResult.removeListener(
  99.         deviceSearchResultHandler);
  100.     chromeHidden.bluetooth.onDeviceSearchFinished.removeListener(
  101.         deviceSearchFinishedHandler);
  102.   }
  103.  
  104.   function maybeFinishDeviceSearch() {
  105.     var state = chromeHidden.bluetooth.getDevicesState;
  106.     if (typeof(state.expectedEventCount) != 'undefined' &&
  107.         state.actualEvents >= state.expectedEventCount) {
  108.       finishDeviceSearch();
  109.     }
  110.   }
  111.  
  112.   function finishDeviceSearch() {
  113.     var finalCallback = chromeHidden.bluetooth.getDevicesState.finalCallback;
  114.     removeDeviceSearchListeners();
  115.     chromeHidden.bluetooth.getDevicesState = null;
  116.  
  117.     if (finalCallback) {
  118.       finalCallback();
  119.     }
  120.   }
  121.  
  122.   apiFunctions.setUpdateArgumentsPostValidate('getDevices',
  123.       function() {
  124.         var args = Array.prototype.slice.call(arguments);
  125.  
  126.         if (chromeHidden.bluetooth.getDevicesState != null) {
  127.           throw new Error("Concurrent calls to getDevices are not allowed.");
  128.         }
  129.  
  130.         var state = { actualEvents: 0 };
  131.  
  132.         if (typeof(args[args.length - 1]) == "function") {
  133.           state.finalCallback = args.pop();
  134.           args.push(
  135.               function() {
  136.                 if (chrome.runtime.lastError) {
  137.                   finishDeviceSearch();
  138.                 }
  139.               });
  140.         } else {
  141.           throw new Error("getDevices must have a final callback parameter.");
  142.         }
  143.  
  144.         if (typeof(args[0].deviceCallback) == "function") {
  145.           state.deviceCallback = args[0].deviceCallback;
  146.         } else {
  147.           throw new Error("getDevices must be passed options with a " +
  148.               "deviceCallback.");
  149.         }
  150.  
  151.         chromeHidden.bluetooth.getDevicesState = state;
  152.         addDeviceSearchListeners();
  153.  
  154.         return args;
  155.       });
  156. });
  157.